//
//  TSHomeController.m
//  TaShuo
//
//  Created by N-X on 2018/1/17.
//  Copyright © 2018年 MRNAN. All rights reserved.
//

#import "TSHomeController.h"
#import "TSSliderShowCell.h"
#import "TSHotAreaViewCell.h"
#import "TSCityCommentCell.h"
#import "TSHomeSectionHeaderView.h"

@interface TSHomeController()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,TSHomePresenterDelegate>
@property (nonatomic ,strong) UICollectionView * collectionView;
@property (nonatomic ,strong)TSHomePresenter * presenter;
@end

@implementation TSHomeController

#pragma mark - init

+ (instancetype)instanceWithPresenter:(TSHomePresenter *)presenter
{
    return [[self alloc] initWithPresenter:presenter];
}

- (instancetype)initWithPresenter:(TSHomePresenter *)presenter
{
    if (self = [super init]) {
        [self commonInit];
        self.presenter = presenter;
        self.presenter.attachView = self;
    }
    return self;
}

- (void)commonInit
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    self.collectionView.backgroundColor = NX_ColorFromRGB(254,198,10);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"TSSliderShowCell" bundle:nil] forCellWithReuseIdentifier:ksliderShowCellID];
    [self.collectionView registerNib:[UINib nibWithNibName:@"TSHotAreaViewCell" bundle:nil] forCellWithReuseIdentifier:kHotAreaCellID];
    [self.collectionView registerNib:[UINib nibWithNibName:@"TSCityCommentCell" bundle:nil] forCellWithReuseIdentifier:kCityCommentID];
    [self.collectionView registerNib:[UINib nibWithNibName:@"TSHomeSectionHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHomeSectionHeaderViewID];
    
    kWeakSelf(self)
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself.presenter refreshData];
    }];
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakself.presenter loadMoreData];
    }];
}

#pragma mark - TSHomePresenterDelegate

- (void)dataWithPresenter:(TSHomePresenter *)presenter didLoadMoreWithResult:(id)result errorArray:(NSArray *)errorArray
{
    [self.collectionView.mj_footer endRefreshing];
    if (!errorArray && !errorArray.count) {
        [self.collectionView reloadData];
    }
}

- (void)dataWithPresenter:(TSHomePresenter *)presenter didRefreshWithResult:(id)result errorArray:(NSArray *)errorArray
{
    [self.collectionView.mj_header endRefreshing];
    if (!errorArray && !errorArray.count) {
        [self.collectionView reloadData];
    }
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            return 1;
        }
        case 1:
        {
            return self.presenter.hotAreaArray.count;
        }
        case 2:
        {
            return self.presenter.cityCommentArray.count;
        }
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            TSSliderShowCell * sliderCell = [collectionView dequeueReusableCellWithReuseIdentifier:ksliderShowCellID forIndexPath:indexPath];
            return sliderCell;
        }
        case 1:
        {
            TSHotAreaViewCell * hotAreaCell = [collectionView dequeueReusableCellWithReuseIdentifier:kHotAreaCellID forIndexPath:indexPath];
            hotAreaCell.tsHotAreaItemDidClickBlock = ^{
                 //点击热门区域item跳转处理
            };
            return hotAreaCell;
        }
        case 2:
        {
            TSCityCommentCell * cityCommentCell = [collectionView dequeueReusableCellWithReuseIdentifier:kCityCommentID forIndexPath:indexPath];
            TSCityCommentPresenter * presenter = self.presenter.cityCommentArray[indexPath.item];
            cityCommentCell.presenter = presenter;
            return cityCommentCell;
        }
    }
    return nil;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        TSHomeSectionHeaderView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kHomeSectionHeaderViewID forIndexPath:indexPath];
        switch (indexPath.section) {
            case 1:
            {
                headerView.title.text = @"热门商圈";
                break;
            }
            case 2:
            {
                headerView.title.text = @"城市热评";
                break;
            }
            default:
                break;
        }
        return headerView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            return CGSizeMake(KScreenWidth, KScreenWidth * 75 / 187.5);
        }
        case 1:
        {
            return  CGSizeMake(KScreenWidth, KScreenWidth * 196.5 / 187.5);
        }
        case 2:
        {
            TSCityCommentPresenter * commentPresenter = self.presenter.cityCommentArray[indexPath.item];
            return  CGSizeMake(KScreenWidth, commentPresenter.cellHeight);//CGSizeMake(KScreenWidth, KScreenWidth * 113 / 187.5);
        }
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            return CGSizeZero;
        }
        case 1:
        {
            return CGSizeMake(KScreenWidth, KScreenWidth * 20 / 187.5);
        }
        case 2:
        {
           return CGSizeMake(KScreenWidth, KScreenWidth * 20 / 187.5);
        }
    }
    return CGSizeZero;
}

@end
